jQuery(document).ready(function ($) {
    //切换content
    $('.titleList span').on('click', function () {
        var $this = $(this)
        if (!$this.hasClass('titleActive')) {
            $('.allitem').animate({ 'marginTop': (-$this.index() * $('.contentItem').height()) })
            $this.siblings().removeClass('titleActive').end().addClass('titleActive')
        }
    })
    //导航栏背景切换
    $(window).scroll(function () {
        if ($(".navbar").offset().top > 50) {
            $(".navbar-fixed-top").addClass("top-nav").css("background-color", "#051728");
        } else {
            $(".navbar-fixed-top").removeClass("top-nav").css("background-color", "");
        }
    });
    //点击导航调到相应的模块
    $('.nav.navbar-nav li a').click(function () {
        $(this).parents('ul').find('a').removeAttr('style');
        $(this).css('color', '#fff');
        var idstr = this.href.substring(this.href.indexOf('#'));
        $('html,body').animate({ scrollTop: $(idstr).offset().top -110 }, 800);
    });

    //动态添加数据
    //  var gamesdata = [{
    //      name: '九黎',
    //      screenshot: '../img/gameimg/jl_jt.jpg',
    //      icon: '../img/gameimg/jl.png',
    //      screentype: 1,
    //      pcurl: 'http://www.91xmy.com/tplayhtml5/tplay_online.html?screentype=1&urlkey=Qj1e2KNDdwPT89BV&gameid=1292&cuid=41'
    //  },
    //{
    //    name: '妈妈把我的游戏藏起来了',
    //    screenshot: '../img/gameimg/mmbwdyycqll_jt.jpg',
    //    icon: '../img/gameimg/mmbwdyycqll.png',
    //    screentype: 0,
    //    pcurl: 'http://www.91xmy.com/tplayhtml5/tplay_online.html?screentype=2&urlkey=z0isgyWPgY3HMbiW&gameid=1291&cuid=41'
    //},
    //{
    //    name: '蛇吃图',
    //    screenshot: '../img/gameimg/stc_jt.jpg',
    //    icon: '../img/gameimg/stc.png',
    //    screentype: 0,
    //    pcurl: 'http://www.91xmy.com/tplayhtml5/tplay_online.html?screentype=1&urlkey=8QMryWzfuSDAQMSu&gameid=1290&cuid=41'
    //},
    //{
    //    name: '贪吃蛇大作战',
    //    screenshot: '../img/gameimg/tcsdzz_jt.jpg',
    //    icon: '../img/gameimg/tcsdzz.png',
    //    screentype: 1,
    //    pcurl: 'http://www.91xmy.com/tplayhtml5/tplay_online.html?screentype=1&urlkey=Q32JZOtzDmDsMpRD&gameid=1289&cuid=41'
    //},
    //{
    //    name: '开心消消乐',
    //    screenshot: '../img/gameimg/kxxxl_jt.jpg',
    //    icon: '../img/gameimg/kxxxl.png',
    //    screentype: 0,
    //    pcurl: 'http://www.91xmy.com/tplayhtml5/tplay.html?screentype=2&urlkey=2XkM8MfYFCfE2KbZ&gameid=1229&cuid=41'
    //}, ];
    //  //动态拼接
    //  var datalen = gamesdata.length;
    //  if (datalen > 0)
    //  {  
    //      for(var index = 0; index < datalen; index++) {
    //          var newslide = $('#swipertemplate').find('.swiper-slide').clone(true);
    //          var obj = gamesdata[index];
    //          newslide.find('.slideimgdiv')
    //              .css({ 'background-image': 'url('+ obj.icon+')' })
    //              .data({ st: obj.screentype, ss: obj.screenshot, pcurl: obj.pcurl });
    //          if (!index) {
    //              newslide.find('.slideimgdiv > div').css({ 'background-color': 'rgba(5,23,40, 0)' });
    //              newslide.find('.slideimgdiv p').css({ 'color': '#fff' }).text(obj.name);
    //              $('#swbtn').data({
    //                  pcurl: obj.pcurl,
    //                  st:obj.screentype
    //              });
    //          }
    //          else
    //          {
    //              newslide.find('.slideimgdiv  p').text(obj.name);
    //          }
    //          $('.swiper-container .swiper-wrapper').append(newslide);
    //      }
    //  }
   
    //games的相册
    var swiper = new Swiper('.swiper-container', {
        slidesPerView: 4,
        spaceBetween: 30,
        freeMode: true,

        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        }
    });
    //顶部相册
    var topswiper = new Swiper('#swiper', {
        loop: true,
        autoplay: true,

        pagination: {
            el: '.swiper-pagination1',
            clickable: true
        },
    });
    //点击跳转到games
    $('.goplay').click(function (e) {
        $('html,body').animate({ scrollTop: $('#games').offset().top -100 }, 800);

    });

    //点击相册切换游戏壁纸，并且绑定修改直接玩的游戏连接

    //2.修改点击试玩的游戏链接
    $('.slideimgdiv div').click(function (e) {

        $('.slideimgdiv div').removeAttr("style");
        $('.slideimgdiv p').removeAttr("style");

        $(this).css('background-color', 'rgba(5,23,40, 0)');
        $(this).find('p').css('color', '#fff');

        var ssimg = "url('" + $(this).parent().data('ss') + "')";
        $('.gamescreenshot').css('background-image', ssimg);

        var pcurl = $(this).parent().data('pcurl');
        var st = $(this).parent().data('st');
        $('.swbtn').data('pcurl', pcurl).data('st', st);
    });


    //点击试玩按钮直接跳转
    $('.swbtn').click(function (e) {

        var url = $(this).data('pcurl');
        var st = $(this).data('st');
        if (url) {
            if (st) {
                $.fancybox.open({
                    href: url,
                    type: 'iframe',
                    padding: 10,
                    width: 640,//896,
                    height: 360,//504,
                    hideOnOverlayClick: false,
                    centerOnScroll: true,
                });
            }
            else {
                $.fancybox.open({
                    href: url,
                    type: 'iframe',
                    padding: 10,
                    width: 360,
                    height: 640,
                    hideOnOverlayClick: false,
                    centerOnScroll: true,
                });
            }

        }
        else {
            alert('该游戏暂不支持试玩！');
        }

    });

    $('#swiper .swiper-pagination-bullet').mouseover(function (e) {
        //获取当前的索引 
        var index= $('#swiper .swiper-pagination-bullet').index(this);
        topswiper.slideTo(index);
        $(this).addClass(':hover');
    });

    $('.clmdiv.clmdiv3').click(function (e) {

        var url = $(this).data('purl');
        window.location.href = url;
    });
    //广告墙上的试玩按钮
    $('.shiwandiv').click(function (e) {
        $('html,body').animate({ scrollTop: $('#games').offset().top - 100 }, 800);
    });

})